'''
文档查重程序入口
'''

from bert_serving.server.helper import get_args_parser
from bert_serving.server import BertServer
from flask import Flask, render_template, request, redirect, url_for
from werkzeug.utils import secure_filename
from interface import interface
import os

# bert-as-serving配置项
args = get_args_parser().parse_args(['-model_dir', './bert-as-service/chinese_L-12_H-768_A-12',
                                     '-port', '5551',
                                     '-port_out', '5552',
                                     # '-max_seq_len', 'NONE',
                                     # '-mask_cls_sep',
                                     '-cpu'])
# 启动flask前先启动bert-as-serving
server = BertServer(args)
server.start()

# 程序配置项
app = Flask(__name__,
            static_url_path='',
            static_folder='templates',
            )

# upload页面
@app.route('/', methods=['POST', 'GET'])
def upload():
    if request.method == 'POST':
        f = request.files['file']
        # 当前文件所在路径
        basepath = os.path.dirname(__file__)
        # 注意：没有的文件夹一定要先创建，不然会提示没有该路径
        upload_path = os.path.join(basepath, 'upload', secure_filename(f.filename))
        f.save(upload_path)
        interface(upload_path, output='templates/result.html')
        return render_template('result.html')
    return render_template('upload.html')


if __name__ == '__main__':
    # 程序入口，ip可以自定义
    app.run(debug=True, host="0.0.0.0")
